﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core.Domain.Directory;
using Nop.Core.Data;
using Nop.Core.Caching;
using Nop.Core.Events;
using Nop.Core;

namespace Nop.Services.ProvinceCitys
{
    public class ProvinceCityService:IProvinceCityService
    {
        #region Constants
        private const string PROVINCECITY_BY_ID_KEY = "Nop.provinceCity.id-{0}";
        private const string PROVINCECITY_PATTERN_KEY = "Nop.provinceCity.";
        #endregion

        #region Fields

        private readonly IRepository<ProvinceCity> _provinceCityRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;

        #endregion

        #region Ctor

        public ProvinceCityService(IRepository<ProvinceCity> provinceCityRepository, ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._provinceCityRepository = provinceCityRepository;
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Inserts a ProvinceCity
        /// </summary>
        /// <param name="ProvinceCity">ProvinceCity</param>
        public virtual void InsertProvinceCity(ProvinceCity provinceCity)
        {
            if (provinceCity == null)
            {
                throw new ArgumentNullException("provinceCity");
            }

            //insert
            _provinceCityRepository.Insert(provinceCity);

            _cacheManager.RemoveByPattern(PROVINCECITY_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(provinceCity);
        }

        /// <summary>
        /// Updates the ProvinceCity
        /// </summary>
        /// <param name="ProvinceCity">ProvinceCity</param>
        public virtual void UpdateProvinceCity(ProvinceCity ProvinceCity)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes a ProvinceCity
        /// </summary>
        /// <param name="ProvinceCity">ProvinceCity</param>
        public virtual void DeleteProvinceCity(ProvinceCity ProvinceCity)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a ProvinceCity
        /// </summary>
        /// <param name="id">The ProvinceCity identifier</param>
        /// <returns></returns>
        public virtual ProvinceCity GetProvinceCityById(int id)
        {
            throw new NotImplementedException();
        }

        public List<ProvinceCity> GetPageList(int pageIndex, int pageSize)
        {
            //var provinceCitys = _provinceCityRepository.Table
            //    .Skip((pageIndex - 1) * pageSize)
            //    .Take(pageSize);
            //return provinceCitys.ToList();

            var query = _provinceCityRepository.Table.OrderBy(pc => pc.Id);
            var list = new PagedList<ProvinceCity>(query, pageIndex, pageSize);
            return list;
        }

        #endregion


        
    }
}
